#include "stdafx.h"
#include "./wincontainer.h"

using namespace std;

HWND WinContainer::CreateEx(std::wstring wstrCaption, int width, int height)	//will center
{
	int cx = ::GetSystemMetrics(SM_CXSCREEN);
	int cy = ::GetSystemMetrics(SM_CYSCREEN);

	RECT rc;
	rc.left = static_cast<LONG>((cx - width) / 2.0);
	rc.top = static_cast<LONG>((cy - height) / 2.0);
	rc.right = rc.left + width;
	rc.bottom = rc.top + height;

	return CreateEx(wstrCaption, rc);
}

HWND WinContainer::CreateEx(std::wstring wstrCaption, RECT rc)
{
	HWND hWnd = Create(NULL, &rc, wstrCaption.c_str());

	return hWnd;
}

Size WinContainer::GetWinSize()
{
	RECT rc;
	::GetClientRect(m_hWnd, &rc);
	
	return Size(rc.right - rc.left, rc.bottom - rc.top);
}

void WinContainer::CloseWindow()
{
	SendMessage(WM_CLOSE, 0, 0);
}

LRESULT WinContainer::MsgOnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	FireEvent(OnWinCreateFunc, OnWinCreate, (GetWinSize()));
	return 0;
}

LRESULT WinContainer::MsgOnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	FireEvent(OnWinSizeFunc, OnWinSize, (GetWinSize()));
	return 0;
}

LRESULT WinContainer::MsgOnEraseBkg(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	return 1L;
}

LRESULT WinContainer::MsgOnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	FireEvent(OnWinCloseFunc, OnWinClose, ());

	::PostMessage(NULL, WM_QUIT, 0, 0);
	bHandled = FALSE;
	return 0;
}

LRESULT WinContainer::MsgOnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	POINT pt;
	GetClientCursorPos(&pt);
	FireEvent(OnWinMouseFunc, OnWinMouseMove, (Point(pt.x, pt.y)));

	return 0;
}

LRESULT WinContainer::MsgOnMouseLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	POINT pt;
	GetClientCursorPos(&pt);
	FireEvent(OnWinMouseFunc, OnWinMouseLButtonDown, (Point(pt.x, pt.y)));

	return 0;
}

LRESULT WinContainer::MsgOnMouseLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	POINT pt;
	GetClientCursorPos(&pt);
	FireEvent(OnWinMouseFunc, OnWinMouseLButtonUp, (Point(pt.x, pt.y)));

	return 0;
}

void WinContainer::GetClientCursorPos(LPPOINT pt)
{
	::GetCursorPos(pt);

	POINT winPos = {0, 0};
	::ClientToScreen(m_hWnd, &winPos);

	pt->x -= winPos.x;
	pt->y -= winPos.y;
}